package org.one.stone.soup.db.engine;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.one.stone.soup.db.indexing.GISDataIndex;
import org.one.stone.soup.db.indexing.Index;
import org.one.stone.soup.db.indexing.KeyValueIndex;
import org.one.stone.soup.db.indexing.TimeDataIndex;
import org.one.stone.soup.db.processors.DBJavascriptEngine;
import org.one.stone.soup.db.processors.JSON;

/**
 * 
 * A database to store large numbers of documents
 * and retrieve them based on criteria.
 *
 */
public class SoupDB {
	
	private static SoupDB database;
	
	public static SoupDB instance() {
		if(database==null) {
			database = new SoupDB();
		}
		return database;
	}
	
	private List<Index> indexes;
	
	private SoupDB() {
		indexes = new ArrayList<Index>();
		indexes.add( new GISDataIndex() );
		indexes.add( new KeyValueIndex() );
		indexes.add( new TimeDataIndex() );
	}
	
	public long store(String key,byte[] data) {
		return 0;
	}
	
	public byte[] retrieve(String key) {
		return null;
	}
	
	public byte[] retrieve(long index) {
		return null;
	}
	
	public OutputStream getStorageStream(String key) {
		return null;
	}
	
	public InputStream getRetrievalStream(String key) {
		return null;
	}
	
	public boolean mountJar(String alias,InputStream jarFileStream,String apiClass,String initialise) {
		return false;
	}
	
	public boolean mountScript(String alias,String script,String initialise) {
		return false;
	}
	
	public boolean executeScript(String alias,String initialise) {
		return false;
	}
	
	public DBJavascriptEngine getJavascriptEngine() {
		return new DBJavascriptEngine();
	}

	public GISDataEngine getGISDataEngine() {
		return new GISDataEngine();
	}

	public void index(long id, JSON json, Class<?> indexClass) {
		((Index) getIndex(indexClass)).index(id, json);
	}

	public <T> T getIndex(Class<T> indexClass) {
		for(Index index: indexes) {
			if(indexClass.isInstance(index)) {
				return (T) index;
			}
		}
		return null;
	}
}
